function formatNumber (n) {
  const str = n.toString()
  return str[1] ? str : `0${str}`
}

//处理时间格式
export function formatTime (date,type) {
  const year = new Date(date).getFullYear()
  const month = new Date(date).getMonth() + 1
  const day = new Date(date).getDate()

  const hour = new Date(date).getHours()
  const minute = new Date(date).getMinutes()
  const second = new Date(date).getSeconds()

  const t1 = [year, month, day].map(formatNumber).join('-')
  const t2 = [hour, minute, second].map(formatNumber).join(':')
  if(type=="yy"){
    return `${t1}`
  }else{
    return `${t1} ${t2}`
  }
}
/*日期加减N天*/
export function addReduce(date,days) {
    if(days===undefined || days===''){
        days = 1;
    }
    let nowDate = date==''?new Date():new Date(date);
    nowDate.setDate(nowDate.getDate()+days);
    return formatTime(nowDate,'yy')
}
export function  addmulMonth(dtstr,n){   // n个月后
  var s=dtstr.split("-");
  var yy=parseInt(s[0]); var mm=parseInt(s[1]-1);var ddd=parseInt(s[2]);
  var dt=new Date(yy,mm,ddd);
  dt.setMonth(dt.getMonth()+n);
  if( (yy*12+dt.getMonth()) > (yy*12+mm + n) )
  {
    dt=new Date(yy,dt.getMonth(),0);
  }

    var month = dt.getMonth()+1;
  var year = yy+1;
  console.log(month)
  if(month==1){
    // month = "0"+1
  }
  if(month<10){
    month = "0"+month
  }
  var days = dt.getDate();
  if(days<10){
	  days = '0'+days
  }
  var dd = year+"-"+month+"-"+days;
  return dd;
}
export function richChange(full){
  let code = ""
  code = (full).replace(/font/g, "span");
  code = (code).replace(/span-/g, "font-");
  code = (code).replace(/color="/g, "style=\"color:");
  code = '<div class="rich" style="padding: 0 20px;font-size:10px">'+code+'</div>';
  return code;
}
export function GetUrlParam(url,paraName) {//获取url参数
  var url = url.toString();
  var arrObj = url.split("?");

  if (arrObj.length > 1) {
    var arrPara = arrObj[1].split("&");
    var arr;

    for (var i = 0; i < arrPara.length; i++) {
      arr = arrPara[i].split("=");

      if (arr != null && arr[0] == paraName) {
        return arr[1];
      }
    }
    return "";
  }
  else {
    return "";
  }
}
/*分割数组*/
export function _chunk(arrData,n) {
  let arr = [];
  for(let i=0;i<arrData.length;i+=n){
    arr.push(arrData.slice(i,i+n))
  }
  return arr;
}
/*时间判断*/
export function isOffLine(lastTime) {
    let tenMinutes = 10 * 60 * 1000;
    if(lastTime != null) {
        /*lastTime = lastTime.toString()
        var lastDate = new Date(lastTime.substring(0,4), lastTime.substring(4,6) - 1, lastTime.substring(6,8),lastTime.substring(8,10),lastTime.substring(10,12), lastTime.substring(12,14));
        var currentDate = new Date();
        return (currentDate.getTime() - lastDate.getTime()) > tenMinutes;*/
        let currentDate = new Date();
        return (currentDate.getTime() - lastTime) > tenMinutes;
    }
    return true;
}
export function isOffLine2(lastTime) {
    let tenMinutes = 3 * 24 *60 *60 *1000;
    if(lastTime != null) {
        let currentDate = new Date();
        return (currentDate.getTime() - new Date(lastTime).getTime()) > tenMinutes;
    }
    return true;
}
export function bMapToQQMap(lng,lat) {

    if (lng == null || lng == '' || lat == null || lat == '')
        return [lng, lat];

    let x_pi = 3.14159265358979324;
    let x = parseFloat(lng) - 0.0065;
    let y = parseFloat(lat) - 0.006;
    let z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * x_pi);
    let theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * x_pi);
    let _lng = (z * Math.cos(theta)).toFixed(7);
    let _lat = (z * Math.sin(theta)).toFixed(7);

    return [_lng, _lat];
}
export default {
  formatNumber,
  formatTime,
  addmulMonth,
  richChange,
  GetUrlParam,
  _chunk,
  isOffLine,
    bMapToQQMap
}
